package com.danco.training.zaneuskaya.annotation;


import java.lang.reflect.Field;

import org.apache.log4j.Logger;

import com.danco.training.zaneuskaya.property.PropertyAccess;
import com.danco.training.zaneuskaya.property.PropertyReader;



public class ConfigPropertyAnalizer {

	private static final Logger logger = Logger.getLogger(ConfigPropertyAnalizer.class);

	public static void configPropertyAnalizer(Object object) {

		for (Field field : object.getClass().getDeclaredFields()) {

			if (field.isAnnotationPresent(ConfigProperty.class)) {
				field.setAccessible(true);
				ConfigProperty annotation = field
						.getDeclaredAnnotation(ConfigProperty.class);

				boolean fileFounded = false;

				
					for (String file : annotation.configName()) {
						if (fileFounded == false) {
						PropertyReader.setFilename(file);
						if (!PropertyReader.getPropertyReaderInstance().getProps().isEmpty()) {
							fileFounded = true;
													
						}
						else {
							break;
						}
					}
				}
				try {
				
					if (annotation.type() == Type.INTEGER  ) {
						
							field.set(object, PropertyAccess
									.getIntProperty(annotation.propertyName()));
						
					}
					else{
					if (annotation.type() == Type.BOOLEAN) {
						
							field.set(object, PropertyAccess
									.getBooleanProperty(annotation
											.propertyName()));
						
					} else {

						
							field.set(object, PropertyAccess
									.getProperty(annotation.propertyName()));
						
					}
					}
				
							

			
			field.setAccessible(false);
			}
			catch (IllegalArgumentException e) {
				logger.error(e.getMessage(), e);
				
			} catch (IllegalAccessException e) {
				logger.error(e.getMessage(), e);
				
			}
		}
	}
}
	
}
